// pages/group/info.js
var app = getApp(),
  a = app.requirejs("core");
Page({
  data:{
    num:1,
    show:false,
    display:false
  },
  onGotUserInfo: function (q) {
    var id = this.id;
    var that = this,
      e = app.getCache("userinfo");
    that.setData({
      display: false
    })
    if (e) {
      return;
    }
    app.getUserInfo(q.detail.userInfo, function (t) {
      if (t != 1000) {
        // that.getList();
        // that.setData({
        //   display: false
        // })
        that.getDetail(id);
      } else {
        that.setData({
          display: true
        })
      }

    });

  },

  onLoad:function(options){
    var that=this;
    getApp().get_menu(function (x) {
      that.setData({
        page_config: getApp().page
      })
    })
    this.id = options.id;
  },
  onShow: function () {
    var id = this.id;
    var e = app.getCache("userinfo");
    if (e) {
      this.getDetail(id);
      this.setData({
        display: false
      })
    }else{
      this.setData({
        display: true
      })
    }
  },
  getDetail:function(id){
    var self = this; 
    a.get('Pintuan/ptGroupDetail',{
      id: id,
      uid: getApp().getCache("userinfo").uid
    },function(res){
      console.log(res)
        if(res.code==0){
          var timeStr = '';
          if (res.info.leftTime > 0) {
            var t = --res.info.leftTime
            var h = Math.floor(t / 60 / 60)
            var m = Math.floor((t - h * 60 * 60) / 60)
            var s = t % 60;
            if (h < 10) h = "0" + h;
            if (m < 10) m = "0" + m;
            if (s < 10) s = "0" + s;
            timeStr = h + ':' + m + ':' + s
            console.log('a:' + timeStr);
            self.setTimeData(res.info.leftTime, id)
          }
          var groupMember = [];
          for (var i = res.info.goods.groupNum - 1; i >= 0; i--) {
            if (res.info.groupMember[i]) {
              groupMember[i] = res.info.groupMember[i]
            } else {
              groupMember[i] = {}
            }
          }
          self.setData({
            groupMember, groupMember,
            groupInfo: res.info,
            leftTime: timeStr,
            leftnum:res.info.goods.groupNum - res.info.leftNum,
            show:true
          })
        }else{
          a.alert(res.msg);
        }
    },true)
    
  },
  setTimeData:function(time,id){
    var self = this;
    var id = id;
    setInterval(function(){
        var t = --time;
        var h =  Math.floor(t/60/60);
        var m = Math.floor((t-h*60*60)/60);
        var s = t%60;
        if(h<10) h = "0"+h;
        if(m<10) m = "0"+m;
        if(s<10) s = "0"+s;
        var timeStr = h+':'+m+':'+s
        if (h==0&&m==0&&s==0) {
          a.get('Pintuan/ptGroupDetail', {
            id: id
          }, function (res) {
              var groupMember=[];
            for (var i = res.info.goods.groupNum - 1; i >= 0; i--) {
                if (res.info.groupMember[i]){
                  groupMember[i] = res.info.groupMember[i]
                }else{
                  groupMember[i]={}
                }
              }
              self.setData({
                groupMember:groupMember,
                groupInfo: res.info,
              })
            
          })
        }
      self.setData({
        leftTime:timeStr
      })
    }, 1000)
  },
  onShareAppMessage:function(options){
    console.log(options)
    var path = '/yb_shop/pages/pintuan/pages/group/detail?id=' + this.data.groupInfo.id
    return {
        title: this.data.groupInfo.goods.name,
        path: path,
        success:function(res){
          console.log(path)
          console.log(res)
        }
      }
  },
  goToHome:function(){
    a.jump('/yb_shop/pages/index/index',2)
  },
  showGoodsDetail:function(e){
    var id = e.currentTarget.dataset.id;
    app.redirect('goods/detail','gid='+id)
  },
  goToBuy:function(){
    var goodsDetail = this.data.groupInfo.goods;
    goodsDetail['num'] = this.data.num;
    goodsDetail['goodsPrice'] = this.data.groupInfo.goods.gprice;
    goodsDetail['buyType'] = 1;
    goodsDetail['groupPid'] = this.data.groupInfo.id;
    //console.log(goodsDetail)
    app.goodsInfo = goodsDetail;
    app.redirect('goods/payfor');
  
  },
  selectProp:function(e){
    var current = e.currentTarget.dataset;
    var pid = current.pid;
    var pname = current.pname;
    var name = current.name;
    var propValue = this.propValue ? this.propValue : [];
    propValue[pid] = {pname:pname,name:name};
    this.propValue = propValue;
    this.setData({
      propValue:propValue
    })
  },
  minus:function(){
    var num = this.data.num > 1 ? --this.data.num : 1
    this.setData({
      num : num
    })
  },
  plus:function(){
    var num = ++this.data.num
    this.setData({
      num : num
    })
  },
  showModal:function(e){
   var showModalStatus = e.currentTarget.dataset.statu== 'open' ? true : false;
    app.showModal(this);
    this.setData({
      showModalStatus:showModalStatus
    })
  }
})